<template>
  <van-icon :name="value ? 'good-job' : 'good-job-o'" :class="{ liked: value === 1 }" @click="onLike" />
</template>

<script>
import { addLikeAPI, deteleLikeAPI } from '@/api/User.js'
export default {
  name: 'LikeArticle',
  props: {
    value: {
      type: Number,
      require: true
    },
    articleId: {
      type: [Number, String, Object],
      require: true
    }
  },
  data() {
    return {}
  },
  methods: {
    async onLike() {
      try {
        let status = 0
        if (this.value === 1) {
          // 点赞取消
          await deteleLikeAPI(this.articleId)
        } else {
          // 点赞成功
          await addLikeAPI(this.articleId)
          status = 1
        }
        // 更新视图
        this.$emit('input', status)
        this.$toast.success(status === 1 ? '点赞成功！' : '取消点赞！')
      } catch (err) {
        this.$toast.fail('点赞失败！')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.liked {
  color: red;
}
</style>
